package com.sinoglobal.ningxia.activity.vip;

import android.content.Intent;
import android.os.Bundle;
import android.text.InputType;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.widget.EditText;
import android.widget.TextView;

import com.sinoglobal.ningxia.R;
import com.sinoglobal.ningxia.activity.base.BaseActivity;
import com.sinoglobal.ningxia.beans.RootVo;
import com.sinoglobal.ningxia.downdata.imp.RemoteImpl;
import com.sinoglobal.ningxia.http.Code;
import com.sinoglobal.ningxia.utils.ItktLog;
import com.sinoglobal.ningxia.utils.MContants;
import com.sinoglobal.ningxia.utils.ValidUtil;
import com.sinoglobal.ningxia.widget.PwdSlipButton;
import com.sinoglobal.ningxia.widget.PwdSlipButton.OnChangedListener;
/**
 * 
* @title: ResetPwdActivity.java 
* @package com.sinoglobal.ningxia.activity.vip 
* @description: 重置密码页面
* @author lms
* @date 2014-6-25 下午4:24:30
 */
public class PasswordReset extends BaseActivity implements OnFocusChangeListener {
	private TextView finish, error;// 完成，错误信息展示
	private EditText pwdEdit;//密码输入框
	private String username;//手机号字符串
	private String pwdStr;//密码字符串
	private PwdSlipButton slipButton;//改变密码样式的滑动按钮

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_vip_reset_pwd);
		openChenJInShi();//开启沉浸式
		init();
		setListener();
	}

	private void setListener() {
		pwdEdit.setOnFocusChangeListener(this);
		finish.setOnFocusChangeListener(this);
		//点击完成
		finish.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				modifyPwd();
			}
		});
		
		//点击滑动按钮
		slipButton.SetOnChangedListener(new OnChangedListener() {

			@Override
			public void OnChanged(boolean CheckState) {
				ItktLog.i("----------->" + CheckState);
				pwdStr = pwdEdit.getText().toString().trim();
				if (CheckState) {
					pwdEdit.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
					pwdEdit.setSelection(pwdStr.length());
				} else {
					pwdEdit.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_NORMAL);
					pwdEdit.setSelection(pwdStr.length());
				}
			}
		});
	}

	private void init() {
		titleView.setText(R.string.reset_pwd_title);
		pwdEdit = (EditText) findViewById(R.id.edit_find_pwd);
		finish = (TextView) findViewById(R.id.reset_finish_tv);
		error = (TextView) findViewById(R.id.error_info);
		slipButton = (PwdSlipButton) findViewById(R.id.slipButton1);
		username = getIntent().getStringExtra("username");
	}

	// 修改密码
	protected void modifyPwd() {
		pwdStr = pwdEdit.getText().toString().trim();
		if (!MContants.EMPTY_STRING.equals(validPwd(pwdStr))) {
			error.setText(validPwd(pwdStr));
			return;
		}
		error.setText(MContants.EMPTY_STRING);
		new ItktAsyncTaskWithDialog<Void, Void, RootVo>(getString(R.string.reseting_pwd), true, true) {

			@Override
			public RootVo before(Void... params) throws Exception {
				return RemoteImpl.getInstance().modifyPwd(username, pwdStr);
			}

			@Override
			public void after(RootVo result) {
				if (null == result) { // 数据为空，显示reLoad
					return;
				}

				if (Code.CODE_SUCCESS != result.getCode()) {
					showShortToastMessage(result.getMessage());
					return;
				}
				Intent intent = new Intent(PasswordReset.this, Login.class);
				intent.putExtra("activity", "resetPwd");
				startActivity(intent);
				finish();
				popAllActivityExceptOne(getClass());
			}

			@Override
			public void exception() {

			}
		}.execute();
	}

	// 验证输入的密码
	protected String validPwd(String pwdStr) {
		return ValidUtil.validPassword(pwdStr);// 校验密码的结果字符串

	}

	@Override
	public void onFocusChange(View v, boolean hasFocus) {
		pwdStr = pwdEdit.getText().toString().trim();
		switch (v.getId()) {
		case R.id.edit_find_pwd://密码格式正确或焦点在密码输入框时，不提示错误信息，否则提示
			if (hasFocus)
				error.setText(MContants.EMPTY_STRING);
			else
				error.setText(validPwd(pwdStr));
			break;
			
		case R.id.reset_finish_tv://密码格式正确或焦点不在完成按钮时，不提示错误信息，否则提示
			if (hasFocus)
				error.setText(validPwd(pwdStr));
			else
				error.setText(MContants.EMPTY_STRING);
			break;
		}
	}
}
